import random
import time
import matplotlib.pyplot as plt


x_arr = []
y_arr = []

with open('output.txt', 'r') as f:
    for line in f:
        x_arr.append(float(line.split(',')[0]))
        y_arr.append(float(line.split(',')[1]))
# 设置中文字体支持（可选）
plt.rcParams['font.sans-serif'] = ['SimHei']  # 用来正常显示中文标签
plt.rcParams['axes.unicode_minus'] = False  # 用来正常显示负号

plt.ion()  # 开启交互模式

fig, ax = plt.subplots(figsize=(8, 6))
ax.set_xlim(20000, 42000)
ax.set_ylim(1000, 40000)
ax.grid(True)

points, = ax.plot([], [], 'ro')  # 空点集
x_data = []
y_data = []

for i in range(500,9999):
    # 生成新点
    # new_x = random.uniform(0, 10)
    # new_y = random.uniform(0, 10)
    
    # 添加到数据集
    x_data.append(x_arr[i])
    y_data.append(y_arr[i])
    
    # 更新点集
    points.set_data(x_data, y_data)
    
    # 更新标题
    ax.set_title(f'已添加 {i+1} 个点')
    
    # 重绘
    fig.canvas.draw()
    fig.canvas.flush_events()
    
    time.sleep(0.01)

plt.ioff()
plt.show()